package com.uplift.mgt.map.baidu;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.uplift.common.Const;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.common.result.impl.LayuiResult;

@Controller
@RequestMapping(value = "/map")
public class BaiduMapController {

	@RequestMapping(value = "/places")
	@ResponseBody
	public IResultable places(HttpServletRequest request) {

		CloseableHttpClient httpClient = HttpClients.createDefault();

		try {
			LayuiResult layuiResult = new LayuiResult();

			String region = request.getParameter("region");
			String query = request.getParameter("query");
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			if (StringUtils.isBlank(page)) {
				page = "0";
			}
			if (StringUtils.isBlank(pageSize)) {
				pageSize = "20";
			}
			if (StringUtils.isBlank(region)) {
				region = "东京";
			}

			// query=肯德基&page_size=10&page_num=0&scope=1&region=东京&output=json&ak=ugC0aYGxoPcuS1wZ9nIoL5q7OFmUk2jy
			StringBuffer searchUrl = new StringBuffer();
			searchUrl.append(Const.MAP_BAIDU_API_PLACE_ABROAD + "?");
			searchUrl.append("query=" + query + "&");
			searchUrl.append("page_size=" + pageSize + "&");
			searchUrl.append("page_num=" + page + "&");
			searchUrl.append("scope=1&");
			searchUrl.append("region=" + region + "&");
			searchUrl.append("output=json&");
			searchUrl.append("ak=" + Const.MAP_BAIDU_AK);

			HttpGet httpGet = new HttpGet(searchUrl.toString());

			CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String resultStr = EntityUtils.toString(httpResponse.getEntity());
				if (StringUtils.isNoneBlank(resultStr)) {
					ObjectMapper objectMapper = new ObjectMapper();
					Map<?, ?> map = objectMapper.readValue(resultStr, Map.class);
					int status = Integer.parseInt(Objects.toString(map.get("status")));
					String message = Objects.toString(map.get("message"));
					int total = Integer.parseInt(Objects.toString(map.get("total")));
					List<?> results = (List<?>) map.get("results");

					layuiResult.setCode(status == 0 ? "1" : "0");
					layuiResult.setMsg(message);
					layuiResult.setCount(total);
					layuiResult.setData(results);
				} else {
					layuiResult.setCode("0");
					layuiResult.setMsg("查询失败");
					layuiResult.setCount(0);
					layuiResult.setData(null);
				}
			} else {
				layuiResult.setCode("0");
				layuiResult.setMsg("查询失败");
				layuiResult.setCount(0);
				layuiResult.setData(null);
			}
			return layuiResult;
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper.FAIL, "查询失败");
		} finally {
			try {
				httpClient.close();// 释放资源
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
